package com.example.blog.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.blog.entity.User;
import com.example.blog.service.UserService;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody User user) {
        return userService.register(user);
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginInfo) {
        String loginName = loginInfo.get("loginName");
        String password = loginInfo.get("password");
        return userService.login(loginName, password);
    }
    
    /**
     * 获取用户信息
     */
    @GetMapping("/{userId}")
    public Map<String, Object> getUserInfo(@PathVariable Integer userId) {
        Map<String, Object> map = new HashMap<>();
        User user = userService.getUserById(userId);
        if (user != null) {
            map.put("success", true);
            map.put("user", user);
        } else {
            map.put("success", false);
            map.put("message", "用户不存在");
        }
        return map;
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public Map<String, Object> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }
    
    /**
     * 检查登录名是否存在
     */
    @GetMapping("/check-login-name")
    public Map<String, Object> checkLoginName(@RequestParam String loginName) {
        Map<String, Object> map = new HashMap<>();
        boolean exists = userService.isLoginNameExists(loginName);
        map.put("exists", exists);
        return map;
    }
    
    /**
     * 检查邮箱是否存在
     */
    @GetMapping("/check-email")
    public Map<String, Object> checkEmail(@RequestParam String email) {
        Map<String, Object> map = new HashMap<>();
        boolean exists = userService.isEmailExists(email);
        map.put("exists", exists);
        return map;
    }
    
    /**
     * 获取当前登录用户的个人资料
     */
    @GetMapping("/profile")
    public Map<String, Object> getUserProfile(@RequestParam(required = false) Integer userId) {
        Map<String, Object> map = new HashMap<>();
        
        if (userId == null) {
            // 如果未提供 userId，返回错误信息
            map.put("success", false);
            map.put("message", "未提供用户 ID");
            return map;
        }
        
        User user = userService.getUserById(userId);
        if (user != null) {
            map.put("success", true);
            map.put("user", user);
        } else {
            map.put("success", false);
            map.put("message", "用户不存在");
        }
        return map;
    }
    
    /**
     * 修改密码
     */
    @PutMapping("/change-password")
    public Map<String, Object> changePassword(@RequestBody Map<String, String> passwordData) {
        Integer userId = null;
        try {
            userId = Integer.parseInt(passwordData.get("userId"));
        } catch (Exception e) {
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("success", false);
            errorMap.put("message", "用户ID无效");
            return errorMap;
        }
        
        String oldPassword = passwordData.get("oldPassword");
        String newPassword = passwordData.get("newPassword");
        
        if (oldPassword == null || newPassword == null) {
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("success", false);
            errorMap.put("message", "请提供旧密码和新密码");
            return errorMap;
        }
        
        return userService.changePassword(userId, oldPassword, newPassword);
    }
} 